{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "854ee4f4",
   "metadata": {},
   "outputs": [],
   "source": [
    "import cv2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "09146d2e",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 创建窗口 WINDOW_AUTOSIZE:自动大小，不允许修改窗口大小\n",
    "cv2.namedWindow('window',cv2.WINDOW_NORMAL)\n",
    "# 更改窗口的大小\n",
    "cv2.resizeWindow('window',800,600)\n",
    "# 展示名字为window的窗口\n",
    "cv2.imshow('window',0)\n",
    "\n",
    "# 等待按键\n",
    "# waitKey会返回按键的ascii的值\n",
    "# 0表示接受任意按键，如果给其他的证书，表示等待按键的时间，单位是毫秒\n",
    "# 可以利用waitkey来销毁窗口，不用每次都重启Python\n",
    "key = cv2.waitKey(0)\n",
    "if key &0xFF== ord('q'):\n",
    "    print(\"准备销毁窗口\")\n",
    "    cv2.destroyAllWindows()\n",
    "    "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b9461db6",
   "metadata": {},
   "source": [
    "### 读取图片"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "cfb162df",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "准备销毁窗口\n"
     ]
    }
   ],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "# 默认是彩色图片\n",
    "cat = cv2.imread('img/cat.jpg')\n",
    "# numpy的ndarray\n",
    "# matplotlib显示图片颜色不一样，因为opencv读进来的数据的通道不是默认的红绿蓝(rgb)，而是BGR\n",
    "# plt.imshow(cat)\n",
    "cv2.imshow('cat',cat)\n",
    "key = cv2.waitKey(0)\n",
    "if key &0xFF== ord('q'):\n",
    "    print(\"准备销毁窗口\")\n",
    "    cv2.destroyAllWindows()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "f2039010",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 把展示图片的代码封装成函数，方便展示\n",
    "def cv_show(name,img):\n",
    "    cv2.imshow(name,img)\n",
    "    key = cv2.waitKey(0)\n",
    "    if key & 0xFF == ord('q'):\n",
    "        cv2.destroyAllWindows()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "51fd4aa7",
   "metadata": {},
   "outputs": [],
   "source": [
    "cv_show('cat',cat)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
